import {apiOne,apiTwo} from "./api"
import  * as echarts from "./echarts.min.js"

// 用柱状图展示2020年消费额最高的5个省份
const barOne = async ()=>{
    let data = (await apiOne()).data.data
    let dataarr = [];
    let dataobj = {};
    let dataname = [];
    let datadate = [];
    data.forEach(element => {
        dataobj[element.provinceName] = 0
    });
    data.forEach(element => {
        dataobj[element.provinceName] = dataobj[element.provinceName] + element.finalTotalAmount 
    });
    for (const key in dataobj) {
        dataarr.push({name:key,data:dataobj[key]})
    }
    dataarr = dataarr.sort((a,b)=>{
        return b.data - a.data
    })
    dataarr = dataarr.splice(0,5)
    dataarr.forEach(element => {
        dataname.push(element.name)
        datadate.push(element.data)
    });
    console.log(dataname,datadate)
    let myEcharts = echarts.init(document.getElementById("mainBox"))
    myEcharts.setOption({
        title:{
            text:"2020年消费额最高的5个省份",
        },
        toolbar:{},
        xAxis:{
            name:"省份",
            data:dataname
        },
        yAxis:{
            name:"消费额"
        },
        series:[
            {
                type:"bar",
                data:datadate
            }
        ]
    })
}

// 用柱状图展示2020年消费额最低的5个省份
const barTwo = async ()=>{
    let data = (await apiOne()).data.data
    let dataarr = [];
    let dataobj = {};
    let dataname = [];
    let datadate = [];
    data.forEach(element => {
        dataobj[element.provinceName] = 0
    });
    data.forEach(element => {
        dataobj[element.provinceName] = dataobj[element.provinceName] + element.finalTotalAmount 
    });
    for (const key in dataobj) {
        dataarr.push({name:key,data:dataobj[key]})
    }
    dataarr = dataarr.sort((a,b)=>{
        return a.data - b.data
    })
    dataarr = dataarr.splice(0,5)
    dataarr.forEach(element => {
        dataname.push(element.name)
        datadate.push(element.data)
    });
    console.log(dataname,datadate)
    let myEcharts = echarts.init(document.getElementById("mainBox"))
    myEcharts.setOption({
        title:{
            text:"2020年消费额最低的5个省份",
        },
        toolbar:{},
        xAxis:{
            name:"省份",
            data:dataname
        },
        yAxis:{
            name:"消费额"
        },
        series:[
            {
                type:"bar",
                data:datadate
            }
        ]
    })
}


//折线图展示每年上架商品数量的变化情况
const lineOne = async ()=>{
    let data = (await apiTwo()).data.data
    let dataarr = [];
    let dataobj = {};
    let dataname = [];
    let datadate = [];
    data.forEach(element => {
        dataobj[element.year] = 0
    });
    data.forEach(element => {
        dataobj[element.year] =  dataobj[element.year]  + 1
    });
    for (const key in dataobj) {
        dataname.push(key)
        datadate.push(dataobj[key])
    }   
    console.log(dataname,datadate)
    let myEcharts = echarts.init(document.getElementById("mainBox"))
    myEcharts.setOption({
        title:{
            text:"每年上架商品数量的变化情况",
        },
        toolbar:{},
        xAxis:{
            name:"省份",
            data:dataname
        },
        yAxis:{
            name:"消费额"
        },
        series:[
            {
                type:"line",
                data:datadate
            }
        ]
    })

}


//用条形图展示2020年平均消费额（四舍五入保留两位小数）最高的5个省份
const barThree = async ()=>{
    let data = (await apiOne()).data.data
    let dataarr = [];
    let dataobj = {};
    let dataobjTwo = {};
    let dataname = [];
    let datadate = [];
    data.forEach(element => {
        dataobj[element.provinceName] = 0
        dataobjTwo[element.provinceName] = 0
    });
    data.forEach(element => {
        dataobj[element.provinceName] = dataobj[element.provinceName] + element.finalTotalAmount
        dataobjTwo[element.provinceName] = dataobjTwo[element.provinceName] + 1;
    });
    for (const key in dataobj) {
        let linsidata = dataobj[key] / dataobjTwo[key]
        linsidata = linsidata.toFixed(2)        
        dataarr.push({name:key,data:linsidata})
    }
    dataarr = dataarr.sort((a,b)=>{
        return b.data - a.data
    })
    dataarr = dataarr.splice(0,5)
    dataarr.forEach(element => {
        dataname.push(element.name)
        datadate.push(element.data)
    });
    console.log(dataname,datadate)
    let myEcharts = echarts.init(document.getElementById("mainBox"))
    myEcharts.setOption({
        title:{
            text:"2020年平均消费额最高的5个省份",
        },
        toolbar:{},
        xAxis:{
            name:"省份",
            data:dataname
        },
        yAxis:{
            name:"消费额"
        },
        series:[
            {
                type:"bar",
                data:datadate
            }
        ]
    })
}

/*
折柱混合图展示2020年各省份平均消费额
（四舍五入保留两位小数）和地区平均消费额（四舍五入保留两位小数）
柱状图展示平均消费额最高的5个省份，
折线图展示这5个省所在的地区的平均消费额变化
*/
const barLine = async ()=>{
    let data = (await apiOne()).data.data
    let dataarr = [];
    let dataobj = {};
    let dataobjTwo = {};
    let dataname = [];
    let datadate = [];
    let diquOneobj = {}
    let diquTwoobj = {}
    let diquarr = []
    let diquname = []
    let diqudata = []
    let dataarrsum = []
    data.forEach(element => {
        dataobj[element.provinceName] = 0
        dataobjTwo[element.provinceName] = 0
        diquOneobj[element.regionName] = 0 
        diquTwoobj[element.regionName] = 0 

    });
    data.forEach(element => {
        dataobj[element.provinceName] = dataobj[element.provinceName] + element.finalTotalAmount
        dataobjTwo[element.provinceName] = dataobjTwo[element.provinceName] + 1;
        diquOneobj[element.regionName] = diquOneobj[element.regionName] + element.finalTotalAmount
        diquTwoobj[element.regionName] = diquTwoobj[element.regionName] +1
    });
    for (const key in dataobj) {
        let linsidata = dataobj[key] / dataobjTwo[key]
        linsidata = linsidata.toFixed(2)        
        dataarr.push({name:key,data:linsidata})
    }
    for (const key in diquOneobj) {
        let linsidata = diquOneobj[key] / diquTwoobj[key]
        linsidata = linsidata.toFixed(2)        
        diquarr.push({name:key,data:linsidata})
    }
    dataarr = dataarr.sort((a,b)=>{
        return b.data - a.data
    })
    diquarr = diquarr.sort((a,b)=>{
        return b.data - a.data
    })
    dataarr = dataarr.splice(0,5)
    diquarr = diquarr.splice(0,5)
    dataarr.forEach(element => {
        dataname.push(element.name)
        datadate.push(element.data)
    });
    diquarr.forEach(e=>{
        diquname.push(e.name)
        diqudata.push(e.data)
    })
    for (let index = 0; index < diquname.length; index++) {
        dataarrsum.push(diquname[index]+ "/" + dataname[index])
    }
    console.log(dataname,datadate,diquname,diqudata)
    let myEcharts = echarts.init(document.getElementById("mainBox"))
    myEcharts.setOption({
        title:{
            text:"2020年各省份平均消费额和地区平均消费额",
        },
        toolbar:{},
        xAxis:{
            name:"省份/地区",
            data:dataarrsum
        },
        yAxis:{
            name:"消费额"
        },
        series:[
            {
                type:"bar",
                data:datadate
            },
            {
                type:"line",
                data:diqudata
            }
        ]
    })
}

export const mainFn = ()=>{
    barLine()
}